var now_end="2020/4/1 10:00:00";
countTime();
//正在抢购倒计时

function countTime() {
    //获取当前时间
    var date = new Date();
    var now_start = date.getTime();
    //设置截止时间

    var endDate = new Date(now_end);
    var end = endDate.getTime();

    //时间差
    var leftTime = end-now_start;
    //定义变量 d,h,m,s保存倒计时的时间
    var h,m,s;
    if (leftTime>0) {
        h = Math.floor(leftTime/1000/60/60%24);
        m = Math.floor(leftTime/1000/60%60);
        s = Math.floor(leftTime/1000%60);
    }else {
        var new_now_time = new Date()
        var year = new_now_time.getFullYear();
        var mouth = new_now_time.getMonth()+1;
        var day = new_now_time.getDate();

        new_now_time.setHours(new_now_time.getHours()+2)
        var hour = new_now_time.getHours();

        var minute = new_now_time.getMinutes();
        var secound = new_now_time.getSeconds();


        if (hour<9){
            now_end = year +"/" + mouth +"/" + day +" " +"0"+ hour +":"+minute+":"+secound
        }else {
            now_end = year +"/" + mouth +"/" + day +" " + hour +":"+minute+":"+secound
        }

        countTime()
    }
    //将倒计时赋值到div中
    // document.getElementById("_d").innerHTML = d+"天";

        $("#now-h").html((h<10?"0"+h:h))
        $("#now-m").html((m<10?"0"+m:m))
        $("#now-s").html((s<10?"0"+s:s))


    /*//设置截止时间
    var now_end="2020/3/31 12:00:00";*/
    var now_end_time = new Date(now_end)
    now_end_time.getHours()-2

    if (parseInt(now_end.substr(9,3))==0){
        $(".underway-time").text(22+":00")
    } else {
        $(".underway-time").text(now_end_time.getHours()-2+":00")
    }
    $(".next_time1").text(now_end_time.getHours()+":00")
    $(".next_time2").text(now_end_time.getHours()+2+":00")
    $(".next_time3").text(now_end_time.getHours()+4+":00")
    $(".next_time4").text(now_end_time.getHours()+6+":00")


    //递归每秒调用countTime方法，显示动态时间效果
    setTimeout(countTime,1000);

}

/*=======================搜索=======================================*/
function searchToggle(obj, evt){
    var container = $(obj).closest('.search-wrapper');

    if(!container.hasClass('active')){
        container.addClass('active');
        evt.preventDefault();
    }
    else if(container.hasClass('active') && $(obj).closest('.input-holder').length == 0){
        container.removeClass('active');
        // clear input
        container.find('.search-input').val('');
        // clear and hide result container when we press close
        container.find('.result-container').fadeOut(100, function(){$(this).empty();});
    }
}
function submitFn(obj, evt){
    value = $(obj).find('.search-input').val().trim();

    _html = "您搜索的关键词： ";
    if(!value.length){
        _html = "关键词不能为空。";
    }
    else{
        _html += "<b>" + value + "</b>";
    }

    $(obj).find('.result-container').html('<span>' + _html + '</span>');
    $(obj).find('.result-container').fadeIn(100);

    evt.preventDefault();
}


/**************************** 后端支持 ***********************************/
getProductInfo(1)
function getProductInfo(pageNum) {
    $.ajax({
        type:"get",
        url:"/fresh/getproInSeckill/"+pageNum,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status == 1){
                var seckillProductList =  result.data.list
                var pageNum = result.data.pageNum//当前页数
                var total = result.data.total//总记录数
                var pages = result.data.pages//总页码
                var prePage = result.data.prePage;//上一页
                var nextPage = result.data.nextPage//下一页

                //页码渲染
                $(".pageNum").text(pageNum)
                $(".total").text(total)
                $(".pages").text(pages)

                if (pageNum==1){
                    $(".prePage").css("display","none")
                }else {
                    $(".prePage").css("display","inline-block")
                    var hrefVal = "javascript:getProductByTime("+(prePage)+")"
                    $(".prePage").attr("href",hrefVal)
                }

                if (pageNum==pages){
                    $(".nextPage").css("display","none")
                }else {
                    $(".nextPage").css("display","inline-block")
                    var hrefVal = "javascript:getProductByTime("+(nextPage)+")"
                    $(".nextPage").attr("href",hrefVal)
                }
                //渲染商品信息
                $.each(seckillProductList,function (index,seckillProduct) {
                    var proName = seckillProduct.product.productName
                    var proId = seckillProduct.product.productId
                    var proImgpath = seckillProduct.productImages.productImagesImage
                    var seckillprice = seckillProduct.seckill.seckillPrice
                    var proPrice = seckillProduct.product.productPrice
                    var seckillTotal = seckillProduct.seckill.seckillTotal
                    var seckillNum = seckillProduct.seckill.seckillNum
                    var processed = (seckillNum/seckillTotal)*100%

                    console.log(processed);

                    var ele = "<li>\n" +
                        "                <a href=\"/fresh/goodsDetail/"+proId+"\" class=\"goods-img\">\n" +
                        "                    <img src=\"/fresh/images/"+proImgpath+"\" alt=\"\">\n" +
                        "                    <p>"+proName+"</p>\n" +
                        "                </a>\n" +
                        "                    <div class='hidden' id='goodsId'>"+proId+"</div>"+
                        "                <div class=\"goods-inf\">\n" +
                        "                    <div>\n" +
                        "                        <span>\n" +
                        "                        <span class=\"now-price\">\n" +
                        "                            <i>￥</i>\n" +
                        "                            <span >"+seckillprice+"</span>\n" +
                        "                        </span>\n" +
                        "                        <span class=\"old-price\">\n" +
                        "                            <i>￥</i>\n" +
                        "                            <del>"+proPrice+"</del>\n" +
                        "                        </span>\n" +
                        "                    </span>\n" +
                        "                        <div class=\"goods-out\">\n" +
                        "                            <span>已售</span>\n" +
                        "                            <span>\n" +
                        "                                <div class=\"progress\" style=\" width: 60px;height: 6px;\">\n" +
                        "                                    <div class=\"progress-bar progress-bar-danger \" role=\"progressbar\" aria-valuenow=\"80\" aria-valuemin=\"0\" aria-valuemax=\"100\" style=\"width: "+processed+"\" >\n" +
                        "                                    </div>\n" +
                        "                                </div>\n" +
                        "                            </span>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"rush-purchase\">\n" +
                        "                        <a href='/fresh/goodsDetail/"+proId+"' style='color: white'>立即抢购</a>\n" +
                        "                    </div>\n" +
                        "                </div>\n" +
                        "\n" +
                        "            </li>"
                    $(".goods-list-ul").append(ele)
                })
            }else {
                console.log(result.data);
            }
        }
    })
}



